<html>
<head>
<title>IMSModelTools.py</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #808080;}
.s1 { color: #a9b7c6;}
.s2 { color: #cc7832;}
.s3 { color: #6897bb;}
.s4 { color: #6a8759;}
</style>
</head>
<body bgcolor="#2b2b2b">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
IMSModelTools.py</font>
</center></td></tr></table>
<pre><span class="s0"># -*- coding: UTF-8 -*-</span>
<span class="s2">from </span><span class="s1">keras.models </span><span class="s2">import </span><span class="s1">Sequential</span><span class="s2">,</span><span class="s1">Model</span><span class="s2">,</span><span class="s1">load_model</span>
<span class="s2">from </span><span class="s1">keras.layers </span><span class="s2">import </span><span class="s1">Convolution1D</span><span class="s2">,</span><span class="s1">Input</span>
<span class="s2">from </span><span class="s1">keras.layers </span><span class="s2">import </span><span class="s1">MaxPool1D</span>
<span class="s2">from </span><span class="s1">keras.layers </span><span class="s2">import </span><span class="s1">GRU</span><span class="s2">,</span><span class="s1">LSTM</span>
<span class="s2">from </span><span class="s1">keras.layers </span><span class="s2">import </span><span class="s1">Dense</span>
<span class="s2">from </span><span class="s1">keras.layers </span><span class="s2">import </span><span class="s1">Flatten</span>
<span class="s2">from </span><span class="s1">keras.utils </span><span class="s2">import </span><span class="s1">plot_model</span>
<span class="s2">from </span><span class="s1">keras.layers </span><span class="s2">import </span><span class="s1">TimeDistributed</span>
<span class="s2">from </span><span class="s1">keras </span><span class="s2">import </span><span class="s1">optimizers</span>
<span class="s2">from </span><span class="s1">DataTools </span><span class="s2">import </span><span class="s1">*</span>
<span class="s2">import </span><span class="s1">numpy </span><span class="s2">as </span><span class="s1">np</span>
<span class="s2">import </span><span class="s1">os</span>


<span class="s2">def </span><span class="s1">ims_model_build(input_shape=(</span><span class="s3">2000</span><span class="s2">,</span><span class="s3">1</span><span class="s1">)):</span>
    <span class="s0"># input_shape = input.shape()</span>
    <span class="s1">cnn = Sequential()</span>
    <span class="s1">cnn.add(Convolution1D(</span><span class="s3">10</span><span class="s2">, </span><span class="s3">20</span><span class="s2">, </span><span class="s1">padding=</span><span class="s4">'same'</span><span class="s2">, </span><span class="s1">activation=</span><span class="s4">'relu'</span><span class="s2">, </span><span class="s1">input_shape=input_shape))</span>
    <span class="s1">cnn.add(MaxPool1D(pool_size=</span><span class="s3">5</span><span class="s1">))</span>
    <span class="s1">cnn.add(Convolution1D(</span><span class="s3">20</span><span class="s2">,</span><span class="s3">10</span><span class="s2">,</span><span class="s1">padding=</span><span class="s4">'same'</span><span class="s2">,</span><span class="s1">activation=</span><span class="s4">'relu'</span><span class="s1">))</span>
    <span class="s1">cnn.add(MaxPool1D(pool_size=</span><span class="s3">5</span><span class="s1">))</span>
    <span class="s1">cnn.add(Flatten())</span>
    <span class="s1">cnn.add(Dense(</span><span class="s3">20</span><span class="s2">,</span><span class="s1">activation=</span><span class="s4">'relu'</span><span class="s1">))</span>
    <span class="s1">ims_model = Sequential()</span>
    <span class="s1">ims_model.add(TimeDistributed(cnn))</span>
    <span class="s0"># ims_model.add(LSTM(units=3, activation='sigmoid'))</span>
    <span class="s0"># ims_model.add(LSTM(units=32,return_sequences=True))</span>
    <span class="s0"># ims_model.add(LSTM(units=32,return_sequences=True))</span>
    <span class="s1">ims_model.add(GRU(</span><span class="s3">32</span><span class="s2">,</span><span class="s1">return_sequences=</span><span class="s2">False</span><span class="s1">))</span>
    <span class="s1">ims_model.add(Dense(</span><span class="s3">1</span><span class="s2">, </span><span class="s1">activation=</span><span class="s4">'sigmoid'</span><span class="s1">))</span>
    <span class="s1">opt = optimizers.Adam(learning_rate=</span><span class="s3">0.0005</span><span class="s1">)</span>
    <span class="s1">ims_model.compile(loss=</span><span class="s4">'mse'</span><span class="s2">, </span><span class="s1">optimizer=opt</span><span class="s2">, </span><span class="s1">metrics=[</span><span class="s4">'accuracy'</span><span class="s1">])</span>
    <span class="s0"># print(ims_model.summary())</span>
    <span class="s2">return </span><span class="s1">ims_model</span>

<span class="s2">def </span><span class="s1">visualize_loss(history</span><span class="s2">, </span><span class="s1">title=</span><span class="s4">&quot;Training and Validation Loss&quot;</span><span class="s1">):</span>
    <span class="s1">loss = history.history[</span><span class="s4">&quot;loss&quot;</span><span class="s1">]</span>
    <span class="s0"># val_loss = history.history[&quot;val_loss&quot;]</span>
    <span class="s1">epochs = range(len(loss))</span>
    <span class="s1">plt.figure()</span>
    <span class="s1">plt.plot(epochs</span><span class="s2">, </span><span class="s1">loss</span><span class="s2">, </span><span class="s4">&quot;b&quot;</span><span class="s2">, </span><span class="s1">label=</span><span class="s4">&quot;Training loss&quot;</span><span class="s1">)</span>
    <span class="s0"># plt.plot(epochs, val_loss, &quot;r&quot;, label=&quot;Validation loss&quot;)</span>
    <span class="s1">plt.title(title)</span>
    <span class="s1">plt.xlabel(</span><span class="s4">&quot;Epochs&quot;</span><span class="s1">)</span>
    <span class="s1">plt.ylabel(</span><span class="s4">&quot;Loss&quot;</span><span class="s1">)</span>
    <span class="s1">plt.legend()</span>
    <span class="s1">plt.savefig(title+</span><span class="s4">'.png'</span><span class="s1">)</span>
    <span class="s1">plt.show()</span>

<span class="s2">if </span><span class="s1">__name__ == </span><span class="s4">'__main__'</span><span class="s1">:</span>
    <span class="s0"># path = '../IMS/3rd_test/4th_test/txt'</span>
    <span class="s1">path = </span><span class="s4">'../IMS/2nd_test/2nd_test'</span>
    <span class="s0"># data_sigma = imsdatasigma(path)</span>
    <span class="s0"># file = '2004.02.12.10.32.39'</span>
    <span class="s0"># filedir = os.path.join(path, file)</span>
    <span class="s1">datalist = getIMSdatalist(path)</span>
    <span class="s1">trainlist</span><span class="s2">, </span><span class="s1">test_list = ims_dataset_split(datalist)</span>
    <span class="s1">testdataset = [datalist[i] </span><span class="s2">for </span><span class="s1">i </span><span class="s2">in </span><span class="s1">test_list]</span>
    <span class="s1">traindataset = [datalist[k] </span><span class="s2">for </span><span class="s1">k </span><span class="s2">in </span><span class="s1">trainlist]</span>
    <span class="s1">train_generator = IMSDataGenerator(traindataset)</span>
    <span class="s1">test_generator = IMSDataGenerator(testdataset)</span>
    <span class="s0"># x = np.random.normal(size=(10, 3, 1, 2000))</span>
    <span class="s0"># x.shape</span>
    <span class="s0"># cnn = Sequential()</span>
    <span class="s0"># cnn.add(Convolution1D(10, 7, padding='same', activation='relu', input_dim=2000))</span>
    <span class="s0"># cnn.add(MaxPool1D(pool_size=2, strides=2))</span>
    <span class="s0"># cnn.add(Flatten())</span>
    <span class="s0"># cnn.add(Dense(1, activation='relu'))</span>
    <span class="s0"># # ims_model = Sequential()</span>
    <span class="s0"># # ims_model.add(TimeDistributed(cnn))</span>
    <span class="s0"># # ims_model.add(GRU(units=3, activation='relu'))</span>
    <span class="s0"># # ims_model.add(Dense(1, activation='sigmoid'))</span>
    <span class="s0"># cnn.compile(loss='MSE', optimizer='adam', metrics=['loss'])</span>
    <span class="s0"># # cnn.fit(test_generator)</span>
    <span class="s0"># print(cnn)</span>
<span class="s1">my_ims_model = load_model(</span><span class="s4">'my_ims_model.h5'</span><span class="s1">)</span>
<span class="s1">history = my_ims_model.fit(train_generator</span><span class="s2">,</span><span class="s1">verbose=</span><span class="s3">1</span><span class="s2">,</span><span class="s1">epochs=</span><span class="s3">10</span><span class="s1">)</span>
<span class="s0"># plot_model(my_ims_model,to_file='my_ims_model.png')</span>
<span class="s1">my_ims_model.save(</span><span class="s4">'my_ims_model-2.h5'</span><span class="s1">)</span>
<span class="s1">test_age_predict = my_ims_model.predict(test_generator)</span>
<span class="s1">time = range(len(datalist))</span>
<span class="s1">age = [data[</span><span class="s3">1</span><span class="s1">] </span><span class="s2">for </span><span class="s1">data </span><span class="s2">in </span><span class="s1">datalist]</span>

<span class="s1">plt.figure()</span>
<span class="s1">plt.plot(time</span><span class="s2">,</span><span class="s1">age</span><span class="s2">,</span><span class="s4">'b'</span><span class="s2">,</span><span class="s1">label=</span><span class="s4">'bearing_degration'</span><span class="s1">)</span>
<span class="s1">plt.scatter(test_list[:len(test_age_predict)]</span><span class="s2">,</span><span class="s1">test_age_predict</span><span class="s2">,</span><span class="s1">c=</span><span class="s4">'r'</span><span class="s2">,</span><span class="s1">marker=</span><span class="s4">'o'</span><span class="s1">)</span>
<span class="s1">plt.savefig(</span><span class="s4">'bearing_degration.png'</span><span class="s1">)</span>
<span class="s1">plt.show()</span>
<span class="s1">visualize_loss(history)</span>


</pre>
</body>
</html>